using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Deepcode.Flux.Core.Collections;
using Deepcode.Flux.Core.Services.Errors;
using Deepcode.Flux.Core.UI.Interfaces;

public partial class Setup_Install_Default : Deepcode.Flux.Core.UI.NonCachePage
{
	#region Panel Management
	/// <summary>
	/// Holds the current panel index
	/// </summary>
	private int PanelIndex
	{
		get
		{
			object o = ViewState["PI"];
			if (o == null) return 0;
			return (int)o;
		}
		set
		{
			ViewState["PI"] = value;
		}
	}
	/// <summary>
	/// Defines the panels that will be used during this setup
	/// </summary>
	private string[] SetupPanels = 
	{
		"Information.ascx",
		"SetupDatabase.ascx",
		"SetupSuperUser.ascx",
		"SetupGeneralInfo.ascx",
		"ConfirmSetup.ascx"
	};

	/// <summary>
	/// Holds the current wizard panel object reference
	/// </summary>
	private IWizardPane CurrentWizardPanel;
	#endregion

	protected void Page_Load(object sender, EventArgs e)
	{
		// Turn off all caching
		this.Response.Cache.SetCacheability(HttpCacheability.NoCache);
		
		// Check that the app hasn't already been configured
		if (AlreadyConfigured()) ErrorService.Service().DoError(StandardError.ALREADY_CONFIGURED);
		
		// Load the panel being used presently
		LoadCurrentPanel();
	}
	#region Utility methods
	/// <summary>
	/// Determines if the application has aleady been configured for the first time...
	/// </summary>
	/// <returns></returns>
	private bool AlreadyConfigured()
	{
		if (System.IO.File.Exists(Server.MapPath("~/Settings/Flux.config"))) return true;
		return false;
	}
	#endregion
	#region Panel Loading and Setup
	/// <summary>
	/// Loads the current wizard panel
	/// </summary>
	private void LoadCurrentPanel()
	{
		_currentPanel.Controls.Clear();
		CurrentWizardPanel = null;

		string ascx = String.Format("~/admin/Setup/Install/Panels/{0}", SetupPanels[PanelIndex]);
		try
		{
			Control c = LoadControl(ascx);
			c.ID = ascx;
			if (!(c is IWizardPane)) throw new Exception("The control does not implement IWizardPane");
			CurrentWizardPanel = ((IWizardPane)c);
			CurrentWizardPanel.Initialise(SetupDictionarySession.Config);
			_currentPanel.Controls.Add(c);
		}
		catch (Exception ex)
		{
			Deepcode.Flux.Core.UI.Controls.PanelBox panelb = new Deepcode.Flux.Core.UI.Controls.PanelBox();
			panelb.Title = "An error has occurred";

			Deepcode.Flux.Core.UI.Controls.PadBox pb = new Deepcode.Flux.Core.UI.Controls.PadBox();
			Label e1 = new Label();
			e1.Text = "Could not load setup panel " + ascx;
			pb.Controls.Add(e1);

			Label e2 = new Label();
			e2.Text = "<p>" + ex.ToString() + "</p>";
			pb.Controls.Add(e2);

			panelb.Controls.Add( pb );
			_currentPanel.Controls.Add(panelb);

			_buttonHost.Visible = false;
		}
		// Setup the buttons
		SetupButtonStates();
	}
	/// <summary>
	/// Sets the buttons to their appropriate state
	/// </summary>
	private void SetupButtonStates()
	{
		btnNext.Visible = false;
		btnPrevious.Visible = false;
		btnFinish.Visible = false;

		if (this.PanelIndex > 0) btnPrevious.Visible = true;
		if (this.PanelIndex < (this.SetupPanels.Length - 1)) btnNext.Visible = true;
		if (this.PanelIndex == (this.SetupPanels.Length - 1)) btnFinish.Visible = true;
	}
	#endregion
	#region Button handlers
	/// <summary>
	/// Next button click
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnNext_Click(object sender, EventArgs e)
	{
		// Ensure page is valid
		if (!Page.IsValid) return;
		// Ensure we can go next
		if (this.PanelIndex >= (this.SetupPanels.Length - 1)) return;
		// Tell the current panel to save
		if (CurrentWizardPanel != null)
			if (!CurrentWizardPanel.FinishForm(SetupDictionarySession.Config)) return;
		// Load a different panel
		this.PanelIndex++;
		LoadCurrentPanel();
	}
	/// <summary>
	/// Previous button click
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnPrevious_Click(object sender, EventArgs e)
	{
		// Ensure we can go previous
		if (this.PanelIndex <= 0) return;
		this.PanelIndex--;
		LoadCurrentPanel();
	}
	/// <summary>
	/// Finish button clicked
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnFinish_Click(object sender, EventArgs e)
	{
		Response.Redirect("do_install.aspx", true);
	}
	#endregion
}
